from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from typing import List

from ..core.database import get_db
from ..utils.deps import get_current_user
from ..models.user import User
from ..schemas.notebook import NotebookCreate, NotebookUpdate, NotebookResponse, NotebookTreeResponse
from ..services.notebook_service import NotebookService

router = APIRouter(prefix="/notebooks", tags=["笔记本"])


@router.get("", response_model=List[NotebookResponse])
async def get_notebooks(
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """获取所有笔记本"""
    notebooks = NotebookService.get_notebooks(current_user.id, db)
    return notebooks


@router.get("/tree", response_model=List[NotebookTreeResponse])
async def get_notebooks_tree(
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """获取笔记本树形结构"""
    notebooks = NotebookService.get_notebooks_tree(current_user.id, db)
    return notebooks


@router.get("/{notebook_id}", response_model=NotebookResponse)
async def get_notebook(
    notebook_id: int,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """获取笔记本详情"""
    notebook = NotebookService.get_notebook(notebook_id, current_user.id, db)
    return notebook


@router.post("", response_model=NotebookResponse, status_code=201)
async def create_notebook(
    notebook_data: NotebookCreate,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """创建笔记本"""
    notebook = NotebookService.create_notebook(notebook_data, current_user.id, db)
    return notebook


@router.put("/{notebook_id}", response_model=NotebookResponse)
async def update_notebook(
    notebook_id: int,
    notebook_data: NotebookUpdate,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """更新笔记本"""
    notebook = NotebookService.update_notebook(notebook_id, notebook_data, current_user.id, db)
    return notebook


@router.delete("/{notebook_id}", status_code=204)
async def delete_notebook(
    notebook_id: int,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """删除笔记本"""
    NotebookService.delete_notebook(notebook_id, current_user.id, db)
    return None

